from rest_framework import serializers
from apps.users.models import User


class AdminUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        # fields = ['id', 'username', 'email', 'mobile']
        fields = ['id', 'username', 'email', 'mobile', 'password']

    def create(self, validated_data):
        # 重写了父类,调用父类.父类方法里并没有对密码加密
        user = super().create(validated_data)

        # 密码单独加密,set_password
        user.set_password(validated_data.get('password'))

        # 改变user的普通管理员信息
        user.is_staff = True
        user.save()
        return user

    # def create(self, validated_data):
    #     user = super().create(validated_data)
    #     user.set_password(validated_data.get('password'))
    #
    #     user.is_staff = True
    #     user.save()
    #     return user
